package com.wiley.service;

import io.appium.java_client.android.AndroidDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.wiley.publicMethod.BasePage;
import com.wiley.publicMethod.LoggerControler;

import java.net.MalformedURLException;

public class MyFavPage extends BasePage {

	LoggerControler log;
	/**
	 * 页面元素
	 */
	//我的
	public static final By mine_item_loc = By.id(BaseId + "mine_item");

	//点击登录按钮
	public static final By login_tv_loc = By.id(BaseId + "login_tv");

	//登录界面：用户
	public static final By login_user_loc = By.id(BaseId + "login_user");

	//登录界面：密码
	public static final By login_password_loc = By.id(BaseId + "login_password");

	//登录界面：登录好豆
	public static final By login_haodou_loc = By.id(BaseId + "login_haodou");
	
	//用户昵称
	public static final By nick_tv_loc = By.id(BaseId + "nick_tv");
	
	//我的收藏
	public static final By my_fav_loc = By.id(BaseId + "my_fav");
	
	//搜索
	public static final By search_loc = By.id(BaseId + "search");
	
	//iv_cover
	public static final By iv_cover_loc = By.id(BaseId + "iv_cover");
	
	//专辑
	public static final By collect_loc = By.xpath("//android.widget.TextView[@text='专辑']");
	
	//话题
	public static final By topic_loc = By.xpath("//android.widget.TextView[@text='话题']");
	
	//商品
	public static final By goods_loc = By.xpath("//android.widget.TextView[@text='商品']");
	
	//创建新分类
	public static final By create_tv_loc = By.id(BaseId + "create_tv");
	
	//分类命名
	public static final By category_name_loc = By.id(BaseId + "name");
	
	//确定
	public static final By save_bt_loc = By.name("确定");
	
	//编辑
	public static final By edit_bt_loc = By.name("编辑");
	
	//编辑命名 edit_item
	public static final By edit_item_loc = By.id(BaseId + "edit_item");
	
	//删除命名 delete_item
	public static final By delete_item_loc = By.id(BaseId + "delete_item");
	
	//确定删除 right_bt
	public static final By right_bt_loc = By.id(BaseId + "right_bt");
	
	//全部删除 delete_all_item
	public static final By delete_all_item_loc = By.id(BaseId + "delete_all_item");
	
	/**
	 * 操作方法
	 */
	//点击我的
	public void clickMineItem(){
		this.clickEle(mine_item_loc);
	}

	//登录按钮
	public void clickLoginButton(){
		this.clickEle(login_tv_loc);
	}

	//定位输入框：用户
	public void inputLoginUser(String username){
		this.sendKeys(login_user_loc,username);
	}
	
	//定位输入框：密码
	public void inputLoginPwd(String pwd){
		this.sendKeys(login_password_loc,pwd);
	}

	//点击登录好豆
	public void clickLoginHaoDou(){
		this.clickEle(login_haodou_loc);
	}
	
	//获取用户昵称
	public String getUserName(){
		WebElement ele = this.findEle(nick_tv_loc);
		return ele.getText();
	}
	
	//点击我的收藏
	public void clickMyFav(){
		super.swipeToUp(1000);
		this.clickEle(my_fav_loc);
	}
	
	//搜索
	public void search(String recipeName, int expectNum){
		this.sendKeys(search_loc, recipeName);
		driver.sendKeyEvent(84);
		int coverNum = this.getElesLength(iv_cover_loc);
		if(coverNum == expectNum){
			log.info("搜索成功！");
		}else{
			log.info("搜索失败！");
		}
	}
	
	//创建新分类
	public void createNewCategory(String categoryName){
		this.clickEle(create_tv_loc);
		this.sendKeys(category_name_loc, categoryName);
		this.clickEle(save_bt_loc);
	}
	
	//编辑分类
	public void editCategory(String categoryName){
		this.clickEle(edit_bt_loc);
		this.clickEle(edit_item_loc);
		this.sendKeys(category_name_loc, categoryName);
		this.clickEle(save_bt_loc);
	}
	
	//删除分类
	public void deleteCategory(){
		this.clickEle(edit_bt_loc);
		this.clickEle(delete_item_loc);
		this.clickEle(right_bt_loc);
	}

	//全选删除（专辑、话题、商品）
	public void deleteAllItems(By locName){
		this.clickEle(locName);
		this.clickEle(edit_bt_loc);
		this.clickEle(delete_all_item_loc);
		this.clickEle(right_bt_loc);
	}
	
	/**
	 * 业务封装
	 * @throws MalformedURLException 
	 */
	//我的收藏
	public void myFavFunc(String username,Class name) throws MalformedURLException {
		log = this.MyLogger(name);
		AndroidDriver driver = super.SetDriver();
		this.clickMineItem();
		if(super.isElementPresent(nick_tv_loc)){ //判断是否是已登录状态
			this.clickMyFav();
			this.editCategory("哈哈哈");
			log.info("该case测试通过");
		}else{
			this.clickLoginButton();
			this.inputLoginUser(username);
			log.info("本次输入的账号是："+username);
			this.inputLoginPwd("fgwvip123");
			this.clickLoginHaoDou();
			if(super.isElementPresent(nick_tv_loc)){  //判断是否登录成功
				this.clickMyFav();
				this.editCategory("哈哈哈");
				log.info("该case测试通过");
			}
		}
	}
}
